<?php
abstract class Base_Form 
{
	protected $options 	= 	array('name'	=>	'');
							   
	protected $schema;
	
	protected $elements = array();
	
	protected static $_tpl;
	
	protected static $_base_tpl;
	
	/**
    * getElements
    * Returns builded elements
    */
	public function getElementsObj(){
		return $this->elements;
	}
	
	/**
    * getElementsData
    * Returns all elements data
    */
	public function getElementsData(){
	
	}
	
	/**
    * setSchema
    * Builds schema and seperates options from fields
    */
	public function setSchema($name, $schema){
		
		if(isset($schema['options'])){
			foreach($schema['options'] as $name => $value){
				$this->setOption($name, $value);
			}
					
			//Options are now seperated, unset options from schema
			unset($schema['options']);
		} else {
			$this->setOption('name', $name);
		}
		
		
		//update schema with name as identifiers if empty
		foreach($schema['fields'] as $name => $value){
			if(!isset($schema['fields'][$name]['name'])){
				$schema['fields'][$name]['name'] = $name;
			}
		}
		
		$this->schema = $schema;
	}
	
			
	/**
    * getOption
    * get an variable form the options array
    */												  
	public function getOption($name)
    {
        if (isset($this->options[$name]))   {
            return $this->options[$name];
        }
    }
	
	/**
    * setOption
    * set options to options array
    */
    public function setOption($name, $value)
    {
        if (isset($this->options[$name])) {
            $this->options[$name] = $value;
        }
    }
	
	/**
    * Build
    * ...
    */
	public function build(){
		foreach($this->schema['fields'] as $name => $value){
			$element = $this->getElement($value);
			
			//Get validators info from element and add validator object to the element.
			$validators = $element->getValidators();
			
			if($validators != ''){
				foreach($validators as $vName => $vValue){
					$element->addBehaviour($this->getValidator($vName, $vValue, &$element));
				}
			}
			
			//Adds element object to the form object
			$this->elements[$name] = $element;
		}
	}	
	
	/**
    * getElements
    * Returns the selected parser element object.
    */
	protected function getElementt($definition){	
		
		// $this->getParserElement($definition['type']);
	
		if(isset($definition['type'])){
			switch ($definition['type']) {
				case "text":
					return $this->getText($definition);
					break;
				case "password":
					return $this->getPassword($definition);
					break;
				default:
					return $this->getText($definition);
					break;
			}
		 } else {
		 	return $this->getText($definition);
		 }
	}
	
	
	/**
    * loadTemplate
    * sets template for usage
    */
	public function loadTemplate(){
	    if (isset(self::$_tpl)) {
            return;
        }

        self::$_tpl = '<?php' . PHP_EOL . PHP_EOL
							. 'class %s extends Base%s{'. PHP_EOL . PHP_EOL
							. '	protected $form;' . PHP_EOL . PHP_EOL
							. '	public function __construct(){' . PHP_EOL
							. '		$this->form = new Zend_Form();'. PHP_EOL
							. '		$this->startForm();' . PHP_EOL . PHP_EOL
							. '		return $this->form;' . PHP_EOL
							. '	}'. PHP_EOL . PHP_EOL
							. '	public function startForm(){' . PHP_EOL
							. '		%s'. PHP_EOL
							. '	}' . PHP_EOL
							. '}'. PHP_EOL
							. '?>';
	}
	
	
	/**
    * loadBaseTemplate
    * sets template for usage
    */
	public function loadBaseTemplate(){
	    if (isset(self::$_base_tpl)) {
            return;
        }

        self::$_base_tpl = '<?php' . PHP_EOL . PHP_EOL
							. 'abstract class Base%s {'. PHP_EOL
							. '%s'. PHP_EOL
							. '}'. PHP_EOL
							. '?>';
	}
		
}
?>